require(['init'], function () {

  var $imgUpload=$("#J_imgupload");
  function imgUpload($container) {
      var $uploadCoverBox = $container.find('.icon-uploadimg')
      var $uploadCoverThumb = $uploadCoverBox.find('img')
      var $uploadInput = $uploadCoverBox.find('input')
      var coverUploadInput = new UploadInput({
        $dom: $uploadInput,
        done: function (uploadImgSrc) {
        var uploadImgFullSrc='/api/file?u=' + uploadImgSrc;
          if (uploadImgSrc) {
            new ImgCropTool({
              imgSrc: uploadImgFullSrc,
              cropSize:[265, 175, 'courseCover'],
              success:function(imgSrc){
                var imgCropSrc = '/api/file?u=' + imgSrc;
                $uploadCoverThumb.attr('src', imgCropSrc);
                $uploadCoverBox.attr('data-value', imgCropSrc);
              }
            })
          }
        }
      });
  }

  imgUpload($imgUpload);

  //获取老师
  var $addStudentBtn =$('#j-addstudent');
  var studentViewList = new ContactViewList({
      hasAvatar: true,
      editable: false
  })
  $addStudentBtn.after(studentViewList.$dom.addClass('inline-block').addClass('cursor-pointer'))
  function studentSelect() {
      ajaxMethod({
          url: '/api/teachers',
          success: function (data) {
              var candidateStudentList = []
              $.each(data, function (i, item) {
                  console.log(item);
                  candidateStudentList.push({
                      id: item.teacheruid,
                      avatar: '../img/avatar.png',
                      name: item.lastname,
                      phone: item.ClassInNo
                  })
              })

              var studentSelector = new ContactSelector({
                  candidateDataList: candidateStudentList,
                  checkedDataList: studentViewList.getDataList(),
                  checkedMax: 1,
                  callback: {
                      submit: function (checkedDataList) {
                          if (!Object.keys(checkedDataList).length) {
                              _Tips(i18next.t('popupbox.pleaseselect'));//请选择
                              return false
                          }
                          console.log(checkedDataList);
                          $addStudentBtn.hide()

                          studentViewList.update(checkedDataList)
                      }
                  }
              })
              studentSelector.$titleText.html(i18next.t('popupbox.addteacher'))//添加老师
              studentSelector.popUpBox.show();
          }
      })
    }
    $addStudentBtn.on('click', function () {
        studentSelect();
    })
    studentViewList.$dom.on('click', function () {
        studentSelect();
    })
  //课程类型
  getcoursetypename()
  function getcoursetypename(){
    var $coursetypename=$('#coursetypename').html('');
    ajaxMethod({
      url:'/api/coursetypes',
      type:'get',
      success:function(data){
        console.log(data)
        var $option=$('<option>'+i18next.t('table.pleasecourse')+'</option>').appendTo($coursetypename);//请选择课程类型
        $.each(data,function(index,it){
          //console.log(it.coursetypename)
          $('<option data-uid='+it.coursetypeuid+'>'+it.coursetypename+'</option>').appendTo($coursetypename);
        })
      }
    })
  }
  //点击编辑传值
  function loadEnrollData(id, callback) {
      ajaxMethod({
          url:'/api/product/'+id,
          success: function (data) {
              $('input[name="title"]').val(data.title);
              $('input[name="id"]').val(data.id);
              $('input[name="price"]').val(data.price);
              $('input[name="duration"]').val(data.duration);
              $('input[name="schedules"]').val(data.schedules);
              $('input[name="isHot"]').val(data.isHot);
              $("#J_img").attr('src', data.cover);
              $('#coursetypename').val(coursertype);
              //$('#master_teacher').val(masterteacher);
              //$('#j-addstudent').html(masterteacher);
              //获取附文本编辑器的内容
              if(data.isHot==1){
                $('[name=isHot]').click();
              }
              if(data.status==2){
                $('[name=status]').click();
              }
              if(data.type == 1) {
                $('#J_coursetypes').find('input[value="1"]').click();
              }else if(data.type == 2){
                $('#J_coursetypes').find('input[value="2"]').click();
              }
              //$('input[name="type"]').val(data.type);
              ue.ready(function(){
                ue.setContent(data.intro);
              });
              $addStudentBtn.attr('data-value', data.master);
              ajaxMethod({
                url: '/api/teacher/' + data.master,
                success: function (data, status) {
                  if(status.code == 0) {
                    var masterTeacher = {}
                    masterTeacher[data.teacheruid] = {
                      avatar: data.photo,
                      id: data.teacheruid,
                      name: data.lastname,
                      phone: data.ClassInNo
                    }
                    studentViewList.update(masterTeacher)
                    studentViewList.$dom.show()
                    $addStudentBtn.hide();
                  }
                }
              })
              ;(typeof callback == 'function') && callback();
          }
      })
  }
//点击添加
var status="";
function bindSubmit(){
  $("#submit_html").on('click',function(){
    if(FormCheck.childrenInputCheck($('#course'))){
      var status=$('[name=status]').prop('checked') ? 2: 1;
      var isHot=$('[name=isHot]').prop('checked') ? 1: 0;
      var id=$('input[name="id"]').val();
      var title=$('input[name="title"]').val();
      var cover = $('#J_uploadCover img').attr('src');
      var price=$('input[name="price"]').val();
      var duration=$('input[name="duration"]').val();
      var schedules=$('input[name="schedules"]').val();
      var coursetype=$('#coursetypename').find("option:selected").attr("data-uid");
      var type = $('#J_coursetypes').find('[name="type"]:checked').val();
      //var master = $('#master_teacher').find("option:selected").attr("data-uid");data-contactid
      var master = $('#j-studencontainer').find("li").attr("data-contactid");
      //附文本编辑器的内容
      var UeditorTEXT=ue.getContent();
      var arr={
          id:id,
          title:title,
          cover:cover,
          price:price,
          master:master,
          duration:duration,
          schedules:schedules,
          coursetype:coursetype,
          type:type,
          isHot:isHot,
          intro:UeditorTEXT,
          status:status
        }
      if (editFlag) {
        ajaxMethod({
            url: '/api/product/'+id,
            type: 'put',
            data: arr,
            success: function (data) {
                //alert("编辑")
              location.replace("addcourse_list.html");

            }
        });

    } else {
        ajaxMethod({
            url: '/api/product',
            type: 'post',
            data: arr,
            success: function (data) {
              //alert("保存");
            location.replace("addcourse_list.html");
            }
        });
      }
    }
  })
}

var ue=UE.getEditor('editor');
var id = location.search.urlToObj().id
var masterteacher = location.search.urlToObj().masterteacher
var coursertype = location.search.urlToObj().coursertype
    var editFlag = false
    if (id) {
        editFlag = true
        loadEnrollData(id, bindSubmit)
        return
    }
    bindSubmit()


})
